package com.jay.tools.web;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.jay.tools.web.webview.IWebVeiwInitializer;
import com.jay.tools.web.webview.WebChromeClientImpl;
import com.jay.tools.web.route.Router;
import com.jay.tools.web.webview.WebVeiwSetting;
import com.jay.tools.web.webview.WebViewClientImpl;
import com.jay.tools.web.route.RouteKeys;

/**
 * author: tanjianpei
 * date: 2018/8/22 022 15:32
 * <p>
 * description: 应用内部调用，访问web页面
 */
public class WebFragmentImpl extends WebFragment implements IWebVeiwInitializer {

    private IPageLoadListener mIPageLoadListener = null;

    public static WebFragmentImpl newInstance(String url) {
        Bundle args = new Bundle();
        args.putString(RouteKeys.URL, url);
        WebFragmentImpl fragment = new WebFragmentImpl();
        fragment.setArguments(args);
        return fragment;
    }

    public void setPageLoadListener(IPageLoadListener listener) {
        this.mIPageLoadListener = listener;
    }

    @Override
    protected Object getLayout() {
        return getWebView();
    }

    @Override
    protected void initLazy(Bundle savedInstanceState) {
        if (null != getUrl()) {
            // 用原生的方式模拟Web跳转并进行页面加载
            Router.getInstance().loadPage(this);
        }
    }

    @Override
    public IWebVeiwInitializer setInitializer() {
        return this;
    }

    @Override
    public WebView initWebView(WebView webView) {
        return new WebVeiwSetting().createWebView(webView);
    }

    @Override
    public WebViewClient initWebViewClient() {
        final WebViewClientImpl client = new WebViewClientImpl(this);
        if (null != mIPageLoadListener) {
            client.setPageLoadListener(mIPageLoadListener);
        }
        return client;
    }

    @Override
    public WebChromeClient initWebChromeClient() {
        return new WebChromeClientImpl();
    }

}
